/*
 * BoggleClient.java
 *
 * Created on 28 april 2008, 16:06
 */

package client.gui;

import client.data.Positie;
import client.controllers.BoggleController;
import client.controllers.BackTracking;
import client.gui.Chatroom;
import client.communication.ClientCommunication;
import client.gui.smilies.ISmileyOntvanger;
import client.gui.smilies.SmileyVerwerker;
import client.gui.smilies.smileyPopup;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import data.Gameroom;
import data.Gebruiker;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;

/**
 *
 * @author  Gunnar Verhulst
 */
public class BoggleClient extends javax.swing.JFrame implements ISmileyOntvanger{
    private static Logger logger = Logger.getLogger(BoggleClient.class);
    private Gebruiker gebruiker;
    private Chatroom chatroom;
    private Gameroom gameroom;
    private Timer t;
    private int min,sec,tijd;
    private JLabel lblTimer;
    private DefaultListModel listModel;
    private Positie[][] b=null;
    private BoggleClient bc;
    private JLabel[][] lblbord = null;
    private int score=0;
    private JLabel[] spelers;
    private smileyPopup smipopup;
        
    
    /** Creates new form BoggleClient */
    public BoggleClient(Gebruiker g, Chatroom chatroom, Gameroom room,boolean oprichter) {
        initComponents();
        this.gebruiker = g;
        this.chatroom = chatroom;
        this.gameroom = room;
        bc = this;
        smipopup = new smileyPopup(this);
        this.setSize(650, 650);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        listModel = new DefaultListModel();
        lstWoorden.setModel(listModel);
        
        if(oprichter)
            BoggleController.getInstance().reset();
        maakSpelBord();
        maakTimerPaneel();
        lblSpelersLijst.setText("<html>Scores:<br/><br/>" + g.getNickname() + ": " + 0 + "<br /></html>");
        pnlSpelBord.revalidate();
        this.setTitle("Boggle spel: welkom " + gebruiker.getNickname() + " in gameroom " + gameroom.getGamenaam() + "!");
        txtChatMessages.setText("-- Welkom in gameroom " + gameroom.getGamenaam() + "! --");
        txtMessage.selectAll();
        txtMessage.requestFocus();
        toonSpelStartenKnop(gebruiker.getNickname().equals(room.getNicknameOprichter()));
        //startTimer();
    }
    
     public BoggleClient() {
        initComponents();
        gebruiker = new Gebruiker();
        gameroom = new Gameroom();
        gebruiker.setNickname("ikke");
        gameroom.setGamenaam("blabla");
        this.setSize(650, 650);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        listModel = new DefaultListModel();
        lstWoorden.setModel(listModel);
        
        BoggleController.getInstance().reset();
        maakSpelBord();
        maakTimerPaneel();
        pnlSpelBord.revalidate();
        pnlTimer.revalidate();
        pnlWoordToevoegen.revalidate();
        this.setTitle("Boggle spel: welkom " + gebruiker.getNickname() + " in gameroom " + gameroom.getGamenaam() + "!");
        txtChatMessages.setText("-- Welkom in gameroom " + gameroom.getGamenaam() + "! --");
        txtMessage.selectAll();
        txtWoord.requestFocus();
        
     }
     
     public void updateScoreLijst(Hashtable<String, Integer> scores) {
        String lijst = "<html>Scores:<br/><br/>";
        int max=0;
        for (String nickname : scores.keySet()) {
            if(max == 0)
                max = scores.get(nickname).intValue();
            else
                if(scores.get(nickname).intValue() > max)
                    max = scores.get(nickname).intValue();
            lijst += nickname + ": " + scores.get(nickname).intValue() + "<br/>";
            if(nickname.equals(gebruiker.getNickname()))
                score = scores.get(nickname).intValue();
                
        }
        lijst += "</html>";
        System.out.println("scorelijst: "+lijst);
        lblSpelersLijst.setText(lijst);
        if(score == max){
            ClientCommunication.getInstance().verhoogBoggleScore(gebruiker.getScoreId(),score);
            JOptionPane.showMessageDialog(this, "U hebt gewonnen");
        }else {
            ClientCommunication.getInstance().verhoogBoggleVerlorenScore(gebruiker.getScoreId(),score);
            JOptionPane.showMessageDialog(this, "U hebt verloren");
        }
    }

    public void toon() {
        this.setVisible(true);
    }
    
    public void toonSpelStartenKnop(boolean toon) {
        btnSpelStarten.setVisible(toon);
    }
    
    public void veranderOprichterSpel(String nieuweOprichter) {
        getGameroom().setNicknameOprichter(nieuweOprichter);

        //Knop spel starten zichtbaar stellen als je zelf de nieuwe oprichter bent
        if (gebruiker.getNickname().equals(nieuweOprichter)) {
            toonSpelStartenKnop(true);
        }
    }

    private void maakSpelBord() {
        pnlSpelBord.setLayout(new GridLayout(4,4));
        lblbord = new JLabel[4][4];
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                lblbord[i][j] = new JLabel("*",JLabel.CENTER);
                lblbord[i][j].setFont(new Font("Arial",1,30));
                lblbord[i][j].setMinimumSize(new Dimension(pnlSpelBord.getWidth()/4,pnlSpelBord.getWidth()/4));
                lblbord[i][j].setPreferredSize(new Dimension(pnlSpelBord.getWidth()/4,pnlSpelBord.getWidth()/4));
                lblbord[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
                pnlSpelBord.add(lblbord[i][j]);
            }
        }
    }
    
    public void vulBordOp(){
        String [][] bord = new String [4][4];
        bord = BoggleController.getInstance().getSpelbord();
        b = new Positie[4][4];
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                b[i][j] = new Positie(i,j,bord[i][j]);
                lblbord[i][j].setText(bord[i][j]);
            }
        }
    }

    private void maakTimerPaneel() {
        tijd = 180;
        min = tijd/60;
        sec = tijd%60;
        pnlTimer.setLayout(new GridLayout(1,1));
        pnlTimer.setMinimumSize(new Dimension(pnlSpelBord.getWidth(),50));
        pnlTimer.setPreferredSize(new Dimension(pnlSpelBord.getWidth(),50));
        pnlTimer.setMaximumSize(new Dimension(pnlSpelBord.getWidth(),50));
        lblTimer = new JLabel((min<10?"0"+min:""+min)+":"+(sec<10?"0"+sec:""+sec),JLabel.CENTER);
        lblTimer.setFont(new Font("Arial",1,30));
        lblTimer.setMinimumSize(new Dimension(pnlTimer.getWidth(),pnlTimer.getHeight()));
        lblTimer.setPreferredSize(new Dimension(pnlTimer.getWidth(),pnlTimer.getHeight()));
        lblTimer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        pnlTimer.add(lblTimer);
        t = new Timer(1000,new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                //...Perform a task...
                if(tijd>=1){
                    tijd--;
                    min = tijd/60;
                    sec = tijd%60;
                    lblTimer.setText((min<10?"0"+min:""+min)+":"+(sec<10?"0"+sec:""+sec));
                }else{
                    t.stop();
//                    for(int i=0; i<listModel.getSize();i++){
//                        if(((String)listModel.getElementAt(i)).length() < 3)
//                            listModel.remove(i);
//                    }
                    //Stap1: nakijken of de spelers dubbele woorden hebben. indien zo deze verwijderen
                    //Stap2: met algoritme nakijken of deze woorden te vormen zijn in het spelbord.
                    //       backtracking algoritme 
                    BackTracking bt;
                    for(int i = 0; i < getListModel().getSize(); i++){
                        bt = new BackTracking((String)getListModel().getElementAt(i),b);
                        bt.setBc(bc);
                        bt.losop();
                    }
                    ArrayList<String> lijst = new ArrayList();
                    for(int i=0; i < listModel.size(); i++){
                        lijst.add((String)listModel.get(i));
                    }
                    //Stap3: woorden doorsturen naar server om te kijken welke bestaan in db. Server
                    //       stuurt terug welke goed zijn.
                    ClientCommunication.getInstance().zendWoorden(getGebruiker().getNickname(), getGameroom().getGamenaam(),lijst);
                    //Stap4: naargelang de terug gestuurde woorden scores geven naar lengte van woorden.
                    
                }
            }}
        );
    }
    
    public void startTimer(){
        logger.info("timer starten");
        t.start();
    }
    
    private void verzendBericht() {
        //Testcode: moet nog aangepast worden
        //ClientCommunication.getInstance().zendInGameChatBericht(getGebruiker().getNickname(), getGameroom().getGamenaam(), txtMessage.getText());
//        toonBerichtInChatVenster(">> " + getGebruiker().getNickname() + " says: " + txtMessage.getText(), false);
//
//        try {
//            StyledDocument doc = txtChatMessages.getStyledDocument();
//            String text = doc.getText(0, doc.getLength());
//            smileyMetDubbelePunt(doc, text);
//
//            txtChatMessages.setStyledDocument(doc);
//        } catch (BadLocationException exc) {
//            System.out.println("Bad Location");
//            exc.printStackTrace();
//        }
        
        
        if (txtMessage.getText().trim().length() > 0) {
            ClientCommunication.getInstance().zendInGameChatBericht(getGebruiker().getNickname(), getGameroom().getGamenaam(), txtMessage.getText());
            toonBerichtInChatVenster(">> " + getGebruiker().getNickname() + " says: " + txtMessage.getText(), false);
            txtMessage.setText("");
        }
    }
    
    public void toonBerichtInChatVenster(String bericht, boolean fromServer) {
        //Berichten van de server in het grijs tonen
        if (!fromServer)  {
            txtChatMessages.append("\n" + bericht, Color.BLACK);
        }

        //Textveld laten scrollen
        txtChatMessages.setCaretPosition(txtChatMessages.getDocument().getLength() - 1);
        try {
            StyledDocument doc = txtChatMessages.getStyledDocument();
            String text = doc.getText(0, doc.getLength());
            String path = "/client/gui/smilies/images/";
            SmileyVerwerker.parseSmileys(path, doc, text, bericht.length());
            txtChatMessages.setStyledDocument(doc);
        } catch (BadLocationException exc) {
            System.out.println("Smilies file: Bad Location");
            System.out.println(exc.getMessage());
        }
    }
    
    public void setSpelGestart(boolean gestart) {
        getGameroom().setSpelGestart(gestart);
    }
    
    public void showMessage(String bericht, Color c) {
        txtChatMessages.setForeground(c);
        txtChatMessages.setText(bericht);
    }
    
    public void setLijst(DefaultListModel l){
        lstWoorden.setModel(l);
    }
    
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtMessage = new javax.swing.JTextField();
        btnVerzenden = new javax.swing.JButton();
        pnlSpelBord = new javax.swing.JPanel();
        pnlWoorden = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstWoorden = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        pnlTegenSpelers = new javax.swing.JPanel();
        btnSpelStarten = new javax.swing.JButton();
        helpLinkLabel1 = new client.gui.controls.HelpLinkLabel();
        lblSpelersLijst = new javax.swing.JLabel();
        pnlTimer = new javax.swing.JPanel();
        pnlWoordToevoegen = new javax.swing.JPanel();
        txtWoord = new javax.swing.JTextField();
        btnToevoegen = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtChatMessages = new client.gui.controls.ColoredTextpane();
        jtbSmilies = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                BoggleClient.this.windowClosing(evt);
            }
        });

        txtMessage.setText("Type your message here");
        txtMessage.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                chatFocusGained(evt);
            }
        });

        btnVerzenden.setText("Verzenden");
        btnVerzenden.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                VerzendBericht(evt);
            }
        });

        pnlSpelBord.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pnlSpelBord.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                chatFocusGained(evt);
            }
        });

        javax.swing.GroupLayout pnlSpelBordLayout = new javax.swing.GroupLayout(pnlSpelBord);
        pnlSpelBord.setLayout(pnlSpelBordLayout);
        pnlSpelBordLayout.setHorizontalGroup(
            pnlSpelBordLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 392, Short.MAX_VALUE)
        );
        pnlSpelBordLayout.setVerticalGroup(
            pnlSpelBordLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 256, Short.MAX_VALUE)
        );

        pnlWoorden.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pnlWoorden.setAlignmentX(0.0F);
        pnlWoorden.setMaximumSize(new java.awt.Dimension(50, 50));

        jScrollPane2.setMaximumSize(new java.awt.Dimension(50, 50));
        jScrollPane2.setPreferredSize(new java.awt.Dimension(120, 50));

        lstWoorden.setFocusable(false);
        jScrollPane2.setViewportView(lstWoorden);

        jLabel1.setText("Woorden:");

        javax.swing.GroupLayout pnlWoordenLayout = new javax.swing.GroupLayout(pnlWoorden);
        pnlWoorden.setLayout(pnlWoordenLayout);
        pnlWoordenLayout.setHorizontalGroup(
            pnlWoordenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlWoordenLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlWoordenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlWoordenLayout.setVerticalGroup(
            pnlWoordenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlWoordenLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
                .addContainerGap())
        );

        pnlTegenSpelers.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        btnSpelStarten.setText("Spel starten");
        btnSpelStarten.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSpelStarten(evt);
            }
        });

        helpLinkLabel1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);

        javax.swing.GroupLayout pnlTegenSpelersLayout = new javax.swing.GroupLayout(pnlTegenSpelers);
        pnlTegenSpelers.setLayout(pnlTegenSpelersLayout);
        pnlTegenSpelersLayout.setHorizontalGroup(
            pnlTegenSpelersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlTegenSpelersLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlTegenSpelersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblSpelersLijst, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                    .addComponent(helpLinkLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                    .addComponent(btnSpelStarten, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlTegenSpelersLayout.setVerticalGroup(
            pnlTegenSpelersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlTegenSpelersLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblSpelersLijst, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 222, Short.MAX_VALUE)
                .addComponent(btnSpelStarten, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(helpLinkLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pnlTimer.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout pnlTimerLayout = new javax.swing.GroupLayout(pnlTimer);
        pnlTimer.setLayout(pnlTimerLayout);
        pnlTimerLayout.setHorizontalGroup(
            pnlTimerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 392, Short.MAX_VALUE)
        );
        pnlTimerLayout.setVerticalGroup(
            pnlTimerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 64, Short.MAX_VALUE)
        );

        pnlWoordToevoegen.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        btnToevoegen.setText("Toevoegen");
        btnToevoegen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToevoegenActionPerformed(evt);
            }
        });

        btnClear.setText("Leeg Maken");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlWoordToevoegenLayout = new javax.swing.GroupLayout(pnlWoordToevoegen);
        pnlWoordToevoegen.setLayout(pnlWoordToevoegenLayout);
        pnlWoordToevoegenLayout.setHorizontalGroup(
            pnlWoordToevoegenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlWoordToevoegenLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtWoord, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnClear)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnToevoegen)
                .addContainerGap(13, Short.MAX_VALUE))
        );
        pnlWoordToevoegenLayout.setVerticalGroup(
            pnlWoordToevoegenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlWoordToevoegenLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(pnlWoordToevoegenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtWoord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnClear)
                    .addComponent(btnToevoegen))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jScrollPane1.setViewportView(txtChatMessages);

        jtbSmilies.setIcon(new javax.swing.ImageIcon(getClass().getResource("/client/gui/smilies/images/1_grin.gif"))); // NOI18N
        jtbSmilies.setAlignmentX(0.5F);
        jtbSmilies.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jtbSmilies.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSmileyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 636, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnVerzenden, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtbSmilies, javax.swing.GroupLayout.PREFERRED_SIZE, 29, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(pnlWoorden, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(pnlWoordToevoegen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(pnlTimer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(pnlSpelBord, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlTegenSpelers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlTegenSpelers, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlTimer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlSpelBord, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlWoordToevoegen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pnlWoorden, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMessage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtbSmilies)
                    .addComponent(btnVerzenden))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void VerzendBericht(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_VerzendBericht
        // TODO add your handling code here:
        verzendBericht();
    }//GEN-LAST:event_VerzendBericht

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        // TODO add your handling code here:
        txtWoord.setText("");
        txtWoord.requestFocus();
    }//GEN-LAST:event_btnClearActionPerformed

    private void btnToevoegenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToevoegenActionPerformed
        // TODO add your handling code here:
        getListModel().add(getListModel().getSize(), txtWoord.getText());
        txtWoord.setText("");
        txtWoord.requestFocus();
    }//GEN-LAST:event_btnToevoegenActionPerformed

    private void chatFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_chatFocusGained
        // TODO add your handling code here:
        txtMessage.selectAll();
    }//GEN-LAST:event_chatFocusGained

    private void windowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_windowClosing
        // TODO add your handling code here:
        //spel leaven
        ClientCommunication.getInstance().leaveGame(getGebruiker().getNickname(), getGameroom().getGamenaam());
        //Bij het afsluiten van de BoggleClient, terug de chatroom zichtbaar zetten
        chatroom.setVisible(true);
        chatroom.toFront();
        this.dispose();
    }//GEN-LAST:event_windowClosing

    private void btnSpelStarten(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSpelStarten
        // TODO add your handling code here:
        btnSpelStarten.setEnabled(false);
        pnlTegenSpelers.revalidate();
        ClientCommunication.getInstance().startSpel(getGameroom().getGamenaam());
        //ClientCommunication.getInstance().setAanDeBeurt(true);
    }//GEN-LAST:event_btnSpelStarten

    private void btnSmileyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSmileyActionPerformed
        // TODO add your handling code here:
        smipopup.show(jtbSmilies,-(int)smipopup.getPreferredSize().getWidth() + jtbSmilies.getWidth(),-(int)smipopup.getPreferredSize().getHeight() + jtbSmilies.getHeight());
    }//GEN-LAST:event_btnSmileyActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                BoggleClient bc = new BoggleClient();
                bc.pack();
                bc.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnSpelStarten;
    private javax.swing.JButton btnToevoegen;
    private javax.swing.JButton btnVerzenden;
    private client.gui.controls.HelpLinkLabel helpLinkLabel1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToggleButton jtbSmilies;
    private javax.swing.JLabel lblSpelersLijst;
    private javax.swing.JList lstWoorden;
    private javax.swing.JPanel pnlSpelBord;
    private javax.swing.JPanel pnlTegenSpelers;
    private javax.swing.JPanel pnlTimer;
    private javax.swing.JPanel pnlWoordToevoegen;
    private javax.swing.JPanel pnlWoorden;
    private client.gui.controls.ColoredTextpane txtChatMessages;
    private javax.swing.JTextField txtMessage;
    private javax.swing.JTextField txtWoord;
    // End of variables declaration//GEN-END:variables

    public Gebruiker getGebruiker() {
        return gebruiker;
    }

    public void setGebruiker(Gebruiker gebruiker) {
        this.gebruiker = gebruiker;
    }

    public Gameroom getGameroom() {
        return gameroom;
    }

    public void setGameroom(Gameroom gameroom) {
        this.gameroom = gameroom;
    }

    public DefaultListModel getListModel() {
        return listModel;
    }

    public void setListModel(DefaultListModel listModel) {
        this.listModel = listModel;
    }

    public void addSmileyToTextField(String s) {
        txtMessage.setText(txtMessage.getText() + s);
    }

    public void unToggleSmileyButton() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
